package com.kinyx.framework.data;

import javax.util.XArrayList;

import org.junit.Assert;
import org.junit.Test;

import com.kinyx.framework.file.KFileReader;

import lombok.AllArgsConstructor;
import lombok.Getter;

public class KJsonUtils_Tester_toJson {

	static class Bean1 {}
	@AllArgsConstructor
	static class Bean2 { String str; }
	@Getter
	@AllArgsConstructor
	static class Bean3 { String str; }

	@Test
	public void test1() {
		Assert.assertEquals(null, KJsonUtils.toJson(null, true, true));
		Assert.assertEquals(null, KJsonUtils.toJson(null, true, false));
		Assert.assertEquals("[]", KJsonUtils.toJson(null, false, true));
		Assert.assertEquals("{}", KJsonUtils.toJson(null, false, false));
	}

	@Test
	public void test2() {
		// 空定义
		Assert.assertEquals("{}", KJsonUtils.toJson(new Bean1(), true, true));
		// 无Getter，字段值无法读取。
		Assert.assertEquals("{}", KJsonUtils.toJson(new Bean2(""), true, true));
		// JavaBean正常解读
		Assert.assertEquals("{\"str\":\"\"}", KJsonUtils.toJson(new Bean3(""), true, true));
		// 列表
		Assert.assertEquals("[{\"str\":\"\"}]", KJsonUtils.toJson(new XArrayList<Bean3>().xAdd(new Bean3("")), true, true));
	}

	@Test
	public void testFile() {
		Assert.assertEquals("{\"foo\":\"bar\"}", KJsonUtils4Jackson.toJsonNode(KFileReader.read("test.json", null)).toString());
	}

}
